﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Response
{
    [Serializable]
    public class RefundResponse : AXmlResponse
    {
        private readonly IRefundApplyInfo apply;

        public RefundResponse(string responseString, ITradeChannel channel,
           GybConfigure config,
           IRefundApplyInfo apply) : base(responseString, channel, config)
        {
            this.apply = apply;
        }

        /// <summary>
        /// 交易缩写
        /// </summary>
        public string TranAbbr { get; set; }

        /// <summary>
        /// 网关流水
        /// </summary>
        public string AcqSsn { get; set; }

        /// <summary>
        /// 商户日期时间
        /// </summary>
        public string MercDtTm { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string TermSsn { get; set; }

        /// <summary>
        /// 终端号
        /// </summary>
        public string TermCode { get; set; }

        /// <summary>
        /// 商户号
        /// </summary>
        public string MercCode { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public string TranAmt { get; set; }

        /// <summary>
        /// 清算日期
        /// </summary>
        public string SettDate { get; set; }

        /// <summary>
        /// 转为结果
        /// </summary>      
        /// <returns></returns>
        public ITradeRefundApplyResult ToResult()
        {
            var state = TradeApplyResultState.Complete_Success;
            var msg = "";
            if (!this.IsSuccess())
            {
                state = TradeApplyResultState.Apply_Error;
                msg = this.ErrorCode;
                return new TradeRefundApplyResult(
                       this.channel,
                       apply.RefundNo,
                       apply.TradeNo,
                       apply.ApplyMoney,
                       0m, null,
                       state,
                       msg);
            }
            return new TradeRefundApplyResult(
                this.channel,
                apply.RefundNo,
                apply.TradeNo,
                decimal.Parse(TranAmt),
                0m, null,
                state,
                msg);
        }
    }
}
